package pt.fct.mpc.lastfm.event;

import java.util.List;
import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class EventsXmlHandler extends DefaultHandler {

	private List<EventData> events = new Vector<EventData>();
	private EventData eventDataSet = null;
	private String position = "";

	public List<EventData> getParsedData() {
		return this.events;
	}

	public void startDocument() throws SAXException {
		this.events = new Vector<EventData>();
	}

	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		position = position.concat("." + localName);
		if (localName.equals("event")) {
			eventDataSet = new EventData();
		}
		if (localName.equals("image")) {
			position = position.concat("." + atts.getValue("size"));
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("event")) {
			events.add(eventDataSet);
		}
		position = position.substring(0, position.lastIndexOf("." + localName));
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	public void characters(char ch[], int start, int length) {
		String str = new String(ch, start, length);
		if (position.compareTo(".lfm.events.event.id") == 0) {
			eventDataSet.setId(str);
		} else if (position.compareTo(".lfm.events.event.title") == 0) {
			eventDataSet.setTitle(str);
		} else if (position.compareTo(".lfm.events.event.venue.name") == 0) {
			eventDataSet.setVenue_name(str);
		} else if (position.compareTo(".lfm.events.event.venue.location.city") == 0) {
			eventDataSet.setCity(str);
		} else if (position
				.compareTo(".lfm.events.event.venue.location.country") == 0) {
			eventDataSet.setCountry(str);
		} else if (position.compareTo(".lfm.events.event.startDate") == 0) {
			eventDataSet.setDate(str);
		} else if (position.compareTo(".lfm.events.event.image.small") == 0) {
			eventDataSet.setImage_small(str);
		} else if (position.compareTo(".lfm.events.event.image.medium") == 0) {
			eventDataSet.setImage_medium(str);
		} else if (position.compareTo(".lfm.events.event.image.large") == 0) {
			eventDataSet.setImage_large(str);
		} else if (position.compareTo(".lfm.events.event.image.extralarge") == 0) {
			eventDataSet.setImage_extralarge(str);
		} else if (position
				.equals(".lfm.events.event.venue.location.point.lat")) {
			eventDataSet.setGeo_lat(str);
		} else if (position
				.equals(".lfm.events.event.venue.location.point.long")) {
			eventDataSet.setGeo_long(str);
		} else if (position.equals(".lfm.events.event.artists.artist")) {
			eventDataSet.addArtist(str);
		} else if (position.equals(".lfm.events.event.artists.headliner")) {
			eventDataSet.setHeadliner(str);
		}
	}
}